define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order_refund/index' + location.search,
                    // add_url: 'order/order_refund/add',
                    // edit_url: 'order/order_refund/edit',
                    // del_url: 'order/order_refund/del',
                    multi_url: 'order/order_refund/multi',
                    import_url: 'order/order_refund/import',
                    table: 'order_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'mold', title: __('Mold'), searchList: {"1":__('Mold 1'),"2":__('Mold 2')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'refund_no', title: __('Refund_no'), operate: 'LIKE'},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE', formatter: Table.api.formatter.search},
                        // {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE'},
                        {field: 'goods_spec_str', title: __('Goods_spec_str'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'order_id', title: __('Order_id')},
                        // {field: 'order_goods_id', title: __('Order_goods_id')},
                        // {field: 'goods_id', title: __('Goods_id')},
                        // {field: 'goods_spec_id', title: __('Goods_spec_id')},
                        // {field: 'num', title: __('Num')},
                        {field: 'pay_price', title: __('Pay_price'), operate:'BETWEEN'},
                        {field: 'refund_price', title: __('Refund_price'), operate:'BETWEEN'},
                        // {field: 'trade_no', title: __('Trade_no'), operate: 'LIKE'},
                        {field: 'cause', title: __('Cause'), operate: 'LIKE'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'content', title: __('Content'), operate: 'LIKE'},
                        // {field: 'express_code', title: __('Express_code'), operate: 'LIKE'},
                        // {field: 'express_name', title: __('Express_name'), operate: 'LIKE'},
                        // {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        {field: 'express', title: __('快递信息'), operate: false, formatter: function (value,row,index) {
                                return row.express_name +"<br>"+ row.express_no;
                            }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'check_time', title: __('Check_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'deliver_time', title: __('Deliver_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'refund_time', title: __('Refund_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: '订单详情',
                                    title: '订单详情',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-sticky-note-o',
                                    url: function (res) {
                                        return 'order/order_refund/detail/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        return true;
                                    }
                                }, {
                                    name: 'agree',
                                    text: '同意',
                                    title: '同意',
                                    classname: 'btn btn-xs btn btn-success btn-view btn-ajax',
                                    icon: 'fa fa-check',
                                    url: function (res) {
                                        return 'order/order_refund/agree/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        // if(row.mold === 2){
                                            if(row.status === 0){
                                                return true;
                                            }
                                        // }
                                    },
                                    refresh:true
                                },{
                                    name: 'refuse',
                                    text: '拒绝',
                                    title: '拒绝',
                                    classname: 'btn btn-xs btn btn-danger btn-view btn-dialog',
                                    icon: 'fa fa-close',
                                    url: function (res) {
                                        return 'order/order_refund/refuse/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        if(row.status === 0){
                                            return true;
                                        }
                                    },
                                    refresh:true

                                },{
                                    name: 'edit_price',
                                    text: '改价',
                                    title: '改价',
                                    classname: 'btn btn-xs btn btn-danger btn-view btn-dialog',
                                    icon: 'fa fa-edit',
                                    url: function (res) {
                                        return 'order/order_refund/edit_price/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        if(row.status === 0){
                                            return true;
                                        }
                                    },
                                    refresh:true

                                },{
                                    name: 'refund',
                                    text: '退款完成',
                                    title: '退款完成',
                                    classname: 'btn btn-xs btn btn-info btn-view btn-ajax',
                                    icon: 'fa fa-credit-card',
                                    url: function (res) {
                                        return 'order/order_refund/refund/ids/' + res.id;
                                    },
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        if(row.mold === 2){
                                            if(row.status === 2){
                                                return true;
                                            }
                                        }else{
                                            if(row.status === 1){
                                                return true;
                                            }
                                        }
                                    },
                                    refresh:true
                                },
                            ],
                            formatter: Table.api.formatter.operate,
                            // formatter:function (value, row, index) {
                            //     var that = $.extend({}, this);
                            //     if(row.status == 1){
                            //         $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                            //         that.table = table;
                            //     }
                            //     return Table.api.formatter.operate.call(that, value, row, index);
                            // }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/order_refund/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/order_refund/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/order_refund/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        refuse: function () {
            Controller.api.bindevent();
        },
        edit_price: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
